package com.wuxiaoqin.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wuxiaoqin.admin.entity.Ordergood;
import com.wuxiaoqin.admin.entity.Orders;
import com.wuxiaoqin.admin.mapper.OrdergoodMapper;
import com.wuxiaoqin.admin.mapper.OrdersMapper;
import com.wuxiaoqin.admin.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuxiaoqin.uitls.respObj.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-11-17
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrdergoodMapper ordergoodMapper;
    @Override
    public List<Orders> getAllOrders() {
        List<Orders> orders = ordersMapper.selectList(new QueryWrapper<Orders>());
        return orders;
    }

    /**
     * 未处理订单
     * @return
     */
    @Override
    public List<Orders> getNoProcessOrders() {
        List<Orders> orders = ordersMapper.selectList(new QueryWrapper<Orders>().eq("status",1));
        return orders;
    }

    /**
     * 处理订单变成已发货
     * @param orderNo
     * @return
     */
    @Override
    public RespBean ordersToFahuo(Integer orderNo) {
        List<Object> objects = ordersMapper.selectObjs(new QueryWrapper<Orders>().eq("orderNo", orderNo).select("status"));
        Integer status = (Integer) (objects.get(0));
        if (status != 1){
           return RespBean.error("订单处理出错啦");
        }
        int update = ordersMapper.update(null, new UpdateWrapper<Orders>()
                .eq("orderNo", orderNo).set("status", 2)
                .set("statusDesc", "已发货"));
        if (update<=0){
            return RespBean.error("订单处理失败");
        }
        return RespBean.success("状态修改成功，请重新查询订单状态");
    }

    /**
     * 未处理订单个数
     * @return
     */
    @Override
    public Integer getCount() {
        Integer integer = ordersMapper.selectCount(new QueryWrapper<Orders>().eq("status", 1));
        return integer;
    }

    /**
     * 订单商品项
     * @param orderNo
     * @return
     */
    @Override
    public RespBean getOrderItem(Integer orderNo) {
        List<Ordergood> ordergoodList = ordergoodMapper.selectList(new QueryWrapper<Ordergood>().eq("orderNo", orderNo));
        int goodCount = 0;
        int goodSpecies = 0;
        for (Ordergood ordergood : ordergoodList) {
            goodCount+= ordergood.getGoodssum();
            goodSpecies+=1;
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("goodCount", goodCount);
        result.put("goodSpecies", goodSpecies);
        result.put("goodList", ordergoodList);
        return RespBean.success("查询成功",result);
    }

    /**
     * 查询单个商品项
     * @param orderNo
     * @return
     */
    @Override
    public RespBean orderById(Integer orderNo) {
        Orders orderNo1 = ordersMapper.selectOne(new QueryWrapper<Orders>().eq("orderNo", orderNo));
        return RespBean.success("成功",orderNo1);
    }
}
